var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		tabnum: 1,
		buydialogHidden: true,
		num: 1,
		isfavorite: false,
		btntype: 1,
		ggselected: [],
		ks: '',
		gwcnum: 1,
		nodata: 0,
		userinfo:[],
		current:0,
	},
	_onLoad: function () {
		var that = this
		var id = 0;
		if (that.options && that.options.id) {
			id = that.options.id
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				id = parseInt(scenes[1]);
			}
		}
		$.get('collage/product', { id: id }, function (res) {
			if (res.status == 0) {
				$.alert(res.msg); return;
			}
			var pagecontent = JSON.parse(res.product.detail);
			var _bannerindex = [];
			for(var i in pagecontent){
				_bannerindex[i] = 0;
			}
			res.sysset.textset = that.data.textset
			that.setData({pagecontent:pagecontent,_bannerindex:_bannerindex});
			that.setData(res);
			that.setData({title:res.product.title,sharepic:res.product.pics[0]})
			setInterval(function(){
				that.setData({nowtime: that.data.nowtime+1})
				that.getdjs();
			},1000)

			wx.setNavigationBarTitle({
				title: res.product.name
			});
			//that._onLoad();
		})
	},
  swiperChange: function (e) {
		var that = this;
		that.setData({
			current: e.detail.current
		})
  }, 
	getdjs:function(){
		var that = this
		var nowtime = that.data.nowtime
		for(var i in that.data.teamList){
			var thisteam = that.data.teamList[i]
			var totalsec = thisteam.createtime*1 + thisteam.teamhour * 3600 - nowtime*1
			if(totalsec <=0){
				that.data.teamList[i].djs = '00时00分00秒';
			}else{
				var houer = Math.floor(totalsec/3600);
				var min = Math.floor((totalsec - houer *3600)/60);
				var sec = totalsec - houer*3600 - min*60
				var djs = (houer<10?'0':'')+houer+'时'+(min<10?'0':'')+min+'分'+(sec<10?'0':'')+sec+'秒';
				that.data.teamList[i].djs = djs;
			}
			that.setData({teamList:that.data.teamList})
		}
	},
	//加入购物车
	buydialogShow: function (e) {
		var btntype = e.currentTarget.dataset.btntype;
		if(btntype ==3){
			this.setData({teamid: e.currentTarget.dataset.teamid})
		}
		this.setData({
			btntype: btntype,
			buydialogHidden: !this.data.buydialogHidden
		})
	},
	buydialogChange: function (e) {
		this.setData({
			buydialogHidden: !this.data.buydialogHidden
		});
	},
	//选择规格
	ggchange: function (e) {
		var idx = e.currentTarget.dataset.idx;
		var itemk = e.currentTarget.dataset.itemk;
		var ggselected = this.data.ggselected
		ggselected[itemk] = idx
		var ks = ggselected.join(',');
		this.setData({
			ggselected: ggselected,
			ks: ks
		})
	},
	tobuy: function (e) {
		var type = e.currentTarget.dataset.type
		var that = this;
		var ks = that.data.ks;
		var proid = that.data.product.id;
		var ggid = that.data.guigelist[ks].id;
		var num = that.data.gwcnum;
		//var prodata = proid + ',' + ggid + ',' + num;
		$.goto('/pages/collage/buy?proid=' + proid + '&ggid=' + ggid + '&num=' + num + '&buytype='+type + (type==3?'&teamid='+that.data.teamid:''));
	},
	
	//加
	gwcplus: function (e) {
		var gwcnum = this.data.gwcnum + 1;
		var ggselected = this.data.ks;
		if (gwcnum > this.data.guigelist[ggselected].stock) {
			$.error('库存不足');
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum + 1
		})
	},
	//减
	gwcminus: function (e) {
		var gwcnum = this.data.gwcnum - 1;
		var ggselected = this.data.ks;
		if (gwcnum <= 0) {
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum - 1
		})
	},
	//输入
	gwcinput: function (e) {
		var ggselected = this.data.ks;
		var gwcnum = parseInt(e.detail.value);
		if (gwcnum < 1) return 1;
		if (gwcnum > this.data.guigelist[ggselected].stock) {
			return this.data.guigelist[ggselected].stock
		}
		this.setData({
			gwcnum: gwcnum
		});
	},
	//收藏操作
	addfavorite: function () {
		var that = this;
		var proid = that.data.product.id;
		$.post('collage/addfavorite', { proid: proid,type:'collage'}, function (data) {
			if (data.status == 1) {
				that.setData({ isfavorite: !that.data.isfavorite });
			}
			$.success(data.msg);
		}, '加载中');

	},
	tabClick: function (e) {
		this.setData({
			tabnum: e.currentTarget.dataset.num
		})
	},
	shareClick:function(){
		this.setData({sharetypevisible:true})
	},
	handleClickMask:function(){
		this.setData({sharetypevisible:false})
	},
	bindGetUserInfo: function (e) {
		var that = this
		var userInfo = e.detail.userInfo
		if (e.detail.userInfo) {
			$.post('pub/userinfo', { userInfo: userInfo,encryptedData:e.detail.encryptedData,iv: e.detail.iv}, function (data) {
				that.showPoster();
			});
		}
	},
	showPoster:function(){
		var that = this;
		that.setData({showposter:true,sharetypevisible:false});
		$.post('collage/getposter',{proid:that.data.product.id},function(data){
			if(data.status==0){
				$.alert(data.msg);
			}else{
				that.setData({posterpic:data.poster})
			}
		},'努力生成中')
	},
	posterDialogClose:function(){
		this.setData({showposter:false});
	}
}))